package org.jeecg.modules.santu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.activemq.Producer;
import org.jeecg.modules.demo.wechat.pojo.QueueMessage;
import org.jeecg.modules.santu.entity.TbGroupMember;
import org.jeecg.modules.santu.mapper.TbGroupMemberMapper;
import org.jeecg.modules.santu.service.ITbGroupMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 群成员
 * @Author: jeecg-boot
 * @Date:   2025-02-24
 * @Version: V1.0
 */
@Service
public class TbGroupMemberServiceImpl extends ServiceImpl<TbGroupMemberMapper, TbGroupMember> implements ITbGroupMemberService {

    @Autowired
    private Producer producer;

    @Autowired
    private TbGroupMemberMapper tbGroupMemberMapper;

    public int updatMoneyBySingnin(TbGroupMember one, Double money){
        return tbGroupMemberMapper.updatMoneyBySingnin(one,money);
    }


    public void sendGroupTextMessage(String roomid,String wxid,String content){
        QueueMessage queueMessage = new QueueMessage();
        queueMessage.setRoomId(roomid);
        queueMessage.setWxid(wxid);
        queueMessage.setContent(content);
        queueMessage.setRepeat(QueueMessage.is_repeat);
        producer.send(Producer.SERVER_SEND_QUEUE,queueMessage);
        queueMessage.setRepeat(QueueMessage.not_repeat);
        producer.send(Producer.SERVER_SEND_QUEUE,queueMessage);
    }

    @Override
    public boolean bangdingBusiness(String roomId, String wxid, String openid) {
        QueryWrapper<TbGroupMember> tbGroupMemberQueryWrapper = new QueryWrapper<>();
        tbGroupMemberQueryWrapper.lambda().eq(TbGroupMember::getGroupId, roomId).eq(TbGroupMember::getWxid,wxid);
        TbGroupMember one = this.getOne(tbGroupMemberQueryWrapper);
        if(one!=null){
            one.setOpenid(openid);
            boolean b = this.updateById(one);
            System.out.println("更新是否成功------->" + b);
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void addMoney(String groupId, String wxid, Double systemCommissionPrice) {
        QueryWrapper<TbGroupMember> tbGroupMemberQueryWrapper = new QueryWrapper<>();
        tbGroupMemberQueryWrapper.lambda().eq(TbGroupMember::getGroupId,groupId).eq(TbGroupMember::getWxid,wxid);
        TbGroupMember one = this.getOne(tbGroupMemberQueryWrapper);
        one.setMoney(one.getMoney() + systemCommissionPrice);
        one.setTotalMoney(one.getTotalMoney() + systemCommissionPrice);
        boolean b = this.updateById(one);
        System.out.println("修改余额状态为----->" + b);
    }

    @Override
    public void addCommissionMoney(String groupId, String wxid, double result) {
        QueryWrapper<TbGroupMember> tbGroupMemberQueryWrapper = new QueryWrapper<>();
        tbGroupMemberQueryWrapper.lambda().eq(TbGroupMember::getGroupId,groupId).eq(TbGroupMember::getWxid,wxid);
        TbGroupMember one = this.getOne(tbGroupMemberQueryWrapper);
        one.setCommission(one.getCommission()+result);
        one.setTotalMoney(one.getTotalMoney() + result);
        boolean b = this.updateById(one);
        System.out.println("修改余额状态为----->" + b);
    }

}
